package com.qc.serverlistener;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.qc.chat.ChatListMember;
import com.qc.common.Constants;
import com.qc.common.DBHandler;
import com.qc.common.DBUtil;
import com.qc.entity.SkillTagEntity;
import com.qc.entity.UserEntity;
import com.qc.service.ConfigService;
import com.qc.service.OrderService;
import com.qc.service.UserService;


@Configuration
public class ProjectInit{
    
	@Autowired
	private DataSource dataSource;
	
	@Resource(name="configService")
    private ConfigService configService;
	
    @PostConstruct
    public void initSysParam()throws Exception{
    	System.out.println("tomcate启动了，初始化开始.............."); 
		ChatListMember.getInstance().txtToCacheMap();
		
		//获取用户的基本信息到内存，这样的话每次推送都知道目标推送的registrationId
		cacheUserInfo();
		cacheSkillTag();
		
		//timerSmsRemind();
		
		//订单行程消息推送
		//sendMessage();
		
		//订单状态时间修改
		//orderSettlement();

		
		System.out.println("tomcate启动了，初始化结束.............."); 
    }

    public void cacheUserInfo() {
    	DBHandler dbHandler = new DBHandler();
    	List<Object> l = new ArrayList<Object>();

		try {
			dbHandler.rs = dbHandler.query(DBUtil.getSql(Constants.SELECT_ALL_USER), l, dataSource);
			while(dbHandler.rs.next()){

				UserEntity entity = new UserEntity();
				entity.setUserId(dbHandler.rs.getLong("user_id"));
				entity.setAvatar(dbHandler.rs.getString("avatar"));
				entity.setNickname(dbHandler.rs.getString("nickname"));
				entity.setMobile(dbHandler.rs.getString("mobile"));
				entity.setRegistrationId(dbHandler.rs.getString("registrationId"));
				
				CacheUtil.userInfoMap.put(entity.getUserId(), entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			dbHandler.closeAll();
		}
    }
    
    public void cacheSkillTag() {
    	DBHandler dbHandler = new DBHandler();
    	List<Object> l = new ArrayList<Object>();

		try {
			dbHandler.rs = dbHandler.query(DBUtil.getSql(Constants.SELECT_ALL_SKILL_TAG), l, dataSource);
			while(dbHandler.rs.next()){

				SkillTagEntity entity = SkillTagEntity.getInstance(dbHandler.rs);
				
				
				CacheUtil.skillTagMap.put(entity.getTagName(), entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			dbHandler.closeAll();
		}
    }
    
    public static void main(String[] args) {
        
        
        /*//得到时间类
          Calendar calendar = Calendar.getInstance();
          //设置时间为 xx-xx-xx 00:00:00
          calendar.set(Calendar.HOUR_OF_DAY, 0); //凌晨1点  
          calendar.set(Calendar.MINUTE, 1);  
          calendar.set(Calendar.SECOND, 0);
          Date date = calendar.getTime();
//          date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 0);
          //一天的毫秒数
          long daySpan = 9000;
          //得到定时器实例
          Timer t = new Timer();
          //使用匿名内方式进行方法覆盖
          t.schedule(new TimerTask() {
              public void run() {
                  //run中填写定时器主要执行的代码块
                  System.out.println("定时器执行..");

              }
          }, date, daySpan); //daySpan是一天的毫秒数，也是执行间隔
      };*/
    }
}
